<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>新华书城会员注册页面</title>
    <%--静态包含base标签，css样式--%>
    <%@include file="/pages/common/header.jsp" %>
    <style type="text/css">
        .login_form {
            height: 420px;
            margin-top: 25px;
        }
    </style>
    <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            /*
            * 注册会员页面验证功能
            * */
            $("#sub_btn").click(function () {
                // 检测用户名称是否合法
                let usernameVal = $("#username").val();
                let usernameRegExp = /^\w{5,12}$/;
                if (!usernameRegExp.test(usernameVal)) {
                    $(".errorMsg").text("用户名称不合法");
                    return false;
                }
                // 检测密码输入是否合法
                let passwordVal = $("#password").val();
                let passwordRegExp = /^\w{5,12}$/;
                if (!passwordRegExp.test(passwordVal)) {
                    $(".errorMsg").text("密码格式不合法");
                    return false;
                }
                // 确认密码是否一致功能
                let confirmPwdVal = $("#repwd").val();
                if (confirmPwdVal !== passwordVal) {
                    $(".errorMsg").text("密码输入不一致");
                    return false;
                }
                // 检测邮箱输入是否合法
                let mailboxVal = $("#mailbox").val();
                let mailboxRegExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (!mailboxRegExp.test(mailboxVal)) {
                    $(".errorMsg").text("邮箱格式不合法");
                    return false;
                }
                // 检测验证码填写是否正确
                let codeVal = $("#code").val();
                codeVal = $.trim(codeVal);
                if (codeVal === "" || codeVal === null) {
                    $(".errorMsg").text("验证码输入错误");
                    return false;
                }
                $(".errorMsg").text("");
            });

            /*
            * 点击验证码图片刷新验证码功能
            * */
            $("#verify").click(function () {
                this.src = "kaptchaServlet?timestamp=" + new Date();
            })

            /*
            * 注册时使用ajax技术实时验证用户名是否可用
            * */
            $("#username").blur(function () {
                // 获取用户名
                let username = this.value;

                $.getJSON("userServlet", "action=ajaxVerifyUsername&username=" + username, function (data) {
                    if (data.existsUsername) {
                        $(".errorMsg").text("用户名称可用");
                        $(".errorMsg").text("");
                    } else {
                        $(".errorMsg").text("用户名称已存在");
                    }
                })
            })
        });
    </script>
</head>
<body>
<div id="login_header">
    <img class="logo_img" alt="" src="static/img/logo.png">
</div>

<div class="login_banner">

    <div id="l_content">
        <span class="login_word">欢迎注册</span>
    </div>

    <div id="content">
        <div class="login_form">
            <div class="login_box">
                <div class="tit">
                    <h1>注册新华书城会员</h1>
                    <span class="errorMsg">${requestScope.msg}</span>
                </div>
                <div class="form">
                    <form action="userServlet" method="post">
                        <input type="hidden" name="action" value="register">
                        <label>用户名称：</label>
                        <input class="itxt" type="text" placeholder="请输入用户名" autocomplete="off" tabindex="1"
                               name="username" id="username"
                               value="${requestScope.username}"/>
                        <br/>
                        <br/>
                        <label>用户密码：</label>
                        <input class="itxt" type="password" placeholder="请输入密码" autocomplete="off" tabindex="1"
                               name="password" id="password"/>
                        <br/>
                        <br/>
                        <label>确认密码：</label>
                        <input class="itxt" type="password" placeholder="请确认密码" autocomplete="off" tabindex="1"
                               name="repwd" id="repwd"/>
                        <br/>
                        <br/>
                        <label>电子邮箱：</label>
                        <input class="itxt" type="text" placeholder="请输入邮箱地址" autocomplete="off" tabindex="1"
                               name="mailbox" id="mailbox"
                               value="${requestScope.mailbox}"/>
                        <br/>
                        <br/>
                        <label>填验证码：</label>
                        <input class="itxt" type="text" style="width: 130px;" name="code" id="code"/>
                        <img id="verify" alt="verify" src="kaptchaServlet" style="float: right; margin-right: 50px"
                             width="80px" height="40px">
                        <br/>
                        <br/>
                        <input type="submit" value="注册" id="sub_btn"/>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<%--静态包含页脚信息--%>
<%@include file="/pages/common/footer.jsp" %>
</body>
</html>